#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
import socket


def get_local_ip():
    """获取当前电脑的内网IPv4地址"""
    hostname = socket.gethostname()
    try:
        # 获取所有与主机名相关的地址信息
        addrs = socket.getaddrinfo(hostname, None, socket.AF_INET)  # 指定只获取IPv4地址
        # 遍历地址信息，返回第一个IPv4地址
        for addr in addrs:
            return addr[4][0]  # addr[4] 是一个元组，其中第一个元素是IP地址
    except Exception as e:
        print(f"Error retrieving local IP address: {e}")
        return None  # 如果没有找到IPv4地址，则返回None


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'noflip_web.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    # 打印当前电脑的内网IP地址
    print("欢迎使用DDQuant-X：配将式量化框架！")
    print(f"当前电脑请通过: http://127.0.0.1:8000/index/home 进行访问。")
    print(f"其他手机、平板、电脑请通过: http://{get_local_ip()}:8000/index/home 进行访问。")
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()
